package com.acane.instagram.controller;

import com.acane.instagram.pojo.ApiResponse;
import com.acane.instagram.pojo.Result;
import com.acane.instagram.pojo.User;
import com.acane.instagram.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.UUID;


@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;


    @GetMapping("/get/list")
    public Result getUserList() {
        return Result.success(userService.list());
    }

    @GetMapping("/get/userName")
    public Result getUserByUserName(@RequestParam String userName) {
        return Result.success(userService.getUserByUserName(userName));
    }

    @GetMapping("/message/list")
    public Result getMessageList(@RequestParam String userName) {
        return Result.success(userService.getMessageList(userName));
    }

    @GetMapping("/judge/followed")
    public Result judgeFollowed(@RequestParam String followingId, @RequestParam String followerId) {
        return Result.success(userService.judgeFollowed(followingId, followerId));
    }

    @GetMapping("/get/{userId}")
    public Result getUserById(@PathVariable String userId) {
        return Result.success(userService.getUserById(userId));
    }

    @GetMapping("/recommend/{userId}")
    public Result getRecommendUserList(@PathVariable String userId) {
        return userService.getRecommendUserList(userId);
    }

    @GetMapping("/{userId}/avatar")
    public ResponseEntity<?> getUserAvatar(
            @PathVariable String userId
    ) {
        try {
            User user = userService.getUserById(userId);
            if (user == null || user.getAvatar() == null) {
                return ResponseEntity.status(404).body(
                        new ApiResponse(404, "未找到头像信息", null)
                );
            }
            return ResponseEntity.ok(
                    new ApiResponse(200, "success", user.getAvatar())
            );
        } catch (Exception e) {
            return ResponseEntity.status(500).body(
                    new ApiResponse(500, "服务器错误: " + e.getMessage(), null)
            );
        }
    }

    @GetMapping("/postCount/get")
    public Result getPostCount(@RequestParam String userId) {
        return Result.success(userService.getPostCount(userId));
    }

    @GetMapping("/followerCount/get")
    public Result getFollowerCount(@RequestParam String userId) {
        return Result.success(userService.getFollowerCount(userId));
    }

    @GetMapping("/followingCount/get")
    public Result getFollowingCount(@RequestParam String userId) {
        return Result.success(userService.getFollowingCount(userId));
    }

    @GetMapping("/search")
    public Result searchUser(@RequestParam String keyword) {
        return Result.success(userService.searchUser(keyword));
    }

    @PostMapping("/update")
    public Result updateUser(@RequestBody User user) {
        boolean isSuccess = userService.updateById(user);
        if (!isSuccess) {
            return Result.failure(500, "更新失败");
        }
        return Result.success(user);
    }

    @PostMapping("/register")
    public Result register(@RequestBody User user) {
        user.setNickName("用户@" + user.getUserName());
        user.setId(UUID.randomUUID().toString().replace("-", ""));
        user.setCreatedTime(Timestamp.valueOf(LocalDateTime.now()).toString());
        user.setIntroduction("设置简介，让更多人了解你");
        // 默认为未知性别
        user.setSex(2);
        boolean isSuccess = userService.save(user);
        if (!isSuccess) {
            return Result.failure(500, "注册失败");
        }
        return Result.success(user);
    }
}